﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

using Ucb.Workshop.Dal;
using Ucb.Workshop.Business;
using Ucb.Workshop.WinApp.View.Reportes.CrystalReports;
using Ucb.Workshop.WinApp.View.GestionPedido;
using System.Globalization;



namespace Ucb.Workshop.WinApp.View.GestionCredito
{
    public partial class CreditoOpc : UserControl
    {
        ProviderManager proveedor;

        public ProviderManager Proveedor
        {
            get { return proveedor; }
            set { proveedor = value; }
        }

        public CreditoOpc()
        {
            InitializeComponent();
            AutoCompleteStringCollection data = new AutoCompleteStringCollection();
            
            
            
            data.Add("Adrian");
            data.Add("Andres");
            data.Add("Pepe");
            data.Add("Pablo");
            txtProveedor.AutoCompleteCustomSource = data;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SelectorProveedor sel = new SelectorProveedor(this);
            sel.ShowDialog();
        }

        public void actualizar()
        {
            txtProveedor.Text = this.Proveedor.Empresa;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //dtIni.Format = DateTimePickerFormat.Custom;
            //dtIni.CustomFormat = "MM/dd/yyyyy";

            //dtFin.Format = DateTimePickerFormat.Custom;
            //dtFin.CustomFormat = "MM/dd/yyyyy";



           string ini = dtIni.Text;
           string fin = dtFin.Text;

            ////string consulta = @"SELECT DISTINCT(c.cod_credito), c.fecha, c.monto, cp.fecha, cp.total "+
           string consulta = @"SELECT DISTINCT(c.cod_credito), prov.empresa, c.fecha, c.monto, cp.fecha as Expr1, cp.total,(cp.total - c.monto)Expr2 " +
                               @"FROM credito c, compra cp, detalle_compra dc, provee pv, proveedor prov " +
                               //@"WHERE c.cod_compra = cp.cod_compra AND cp.cod_compra = dc.cod_compra AND  dc.cod_provee = pv.cod_provee AND pv.cod_proveedor = prov.cod_proveedor AND c.fecha  between '" + ini + "' AND '" + fin + "'";
           @"WHERE c.cod_compra = cp.cod_compra AND cp.cod_compra = dc.cod_compra AND  dc.cod_provee = pv.cod_provee AND pv.cod_proveedor = prov.cod_proveedor AND c.fecha  between '" + ini + "' AND '" + fin + "'" + " AND prov.empresa = '" + txtProveedor.Text + "'";
           //@"WHERE c.cod_compra = cp.cod_compra AND cp.cod_compra = dc.cod_compra AND  dc.cod_provee = pv.cod_provee AND pv.cod_proveedor = prov.cod_proveedor AND c.fecha  between '" + ini + "' AND '" + fin + "' AND prov.empresa LIKE '%" + txtProveedor.Text.Trim() + "%'";

            #region consulta gral
            cr_creditos rep = new cr_creditos();
            #endregion
            SqlDataAdapter ad = new SqlDataAdapter(consulta, DBManager.Conexion());

            DataSet ds = new DataSet();
            ad.Fill(ds, "DataTable1");
            try
            {
                rep.SetDataSource(ds);
                crystalReportViewer1.ReportSource = rep;
                crystalReportViewer1.ShowExportButton = true;

            }
            catch
            { }

            ad.Dispose();   
        }

        private void dtIni_ValueChanged(object sender, EventArgs e)
        {

        }
    }
}
